package com.iweb.controller;

import com.iweb.constant.UrlRequest;
import com.iweb.utils.UrlUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/upload/*")
@MultipartConfig
public class UploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = UrlUtil.url(req.getRequestURI(), UrlRequest.UPLOAD_URL);
        if ("/avatar".equals(action)) handlerAvatar(req, resp);
    }

    // TO DO
    private void handlerAvatar(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("user-avatar");
        // 获取Part集合

        // 判断单个文件上传

        // 获取Part对象

        // 获取文件名称 image-20200511141059682.png

        // 获取文件的后缀

        // 重新封装文件名称 ： "avatar-"+userId+”.png/jpg“

        // 保存文件到指定的nginx路径

        // 封装 客户端访问的 http 路径

        // 返回json格式 {"message":"上传成功","src":"http的访问路径"}

    }
}
